<template>
  <div class="panel">
    <div class="topremmend" v-if="menu.length !== 0">
      <div class="tabBox">
        <span
          class="item menu-item textOverflow"
          :class="currentId == item.store_category_id ? 'active' : ''"
          v-for="(item, index) in menu"
          :key="index"
          @click="changeType(item.store_category_id)"
        >
          {{ item.home_cate_name }}
        </span>
      </div>
      <div class="panel-content swiperBox">
        <swiper class="swiper" :options="panelSwiper">
          <swiper-slide class="item" v-for="(item, index) in list" :key="index">
            <router-link target="_blank" :to="{ name: 'ServiceProduct', params: { id: item.product_id } }">
              <div class="pic-box"><img :src="item.image" :alt="item.store_name" /></div>
              <h3 class="item-title overflow transition">{{ item.store_name }}</h3>
              <div class="price clearfix">
                <div class="left">
                  <span>￥{{ item.ot_price || 0 }}/Pc</span>
                  <span class="gray">起</span>
                </div>
                <div class="right heart" :class="{ active: item.is_hide_product === 1 }" @click.prevent.stop="collect(item.product_id)"></div>
              </div>
              <div class="cart clearfix">
                <div class="left">
                  <span>{{ item.price || 0 }} Pcs</span>
                  <span class="gray">MOQ</span>
                </div>
                <div class="right" @click.prevent.stop="cart(item.product_id)"><img src="~/assets/images/cart-icon.png" alt="" /></div>
              </div>
            </router-link>
          </swiper-slide>
        </swiper>
        <div class="botton-con">
          <div class="panel-paginations" slot="pagination"></div>
          <div class="into">
            <router-link
              target="_blank"
              :to="{
                name: 'Topic',
                query: { cateId: currentId, type: 'service' }
              }"
            >
              <span>进入专题</span>
              <img src="~/assets/images/into.png" alt="" />
            </router-link>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Vue from 'vue';
import { Tab, Tabs } from 'vant';
import { swiper, swiperSlide } from 'vue-awesome-swiper';
import 'swiper/dist/css/swiper.css';
import { getProSpecialCate, getcategoryProduct, collectPro, addCart } from '@/request/api';
Vue.use(Tab).use(Tabs);

export default {
  name: 'Panel',
  components: {
    swiper,
    swiperSlide
  },
  data() {
    return {
      menu: [],
      list: [],
      currentId: '',
      panelSwiper: {
        slidesPerView: 6,
        slidesPerColumn: 2,
        slidesPerGroup: 12,
        spaceBetween: 10,
        slidesPerColumnFill: 'row',
        observer: true,
        pagination: {
          el: '.panel-paginations',
          clickable: true
        }
      }
    };
  },
  mounted() {
    this.getMenu();
  },
  methods: {
    changeType(id) {
      this.currentId = id;
      this.getList();
    },
    getList() {
      this.list = [];
      getcategoryProduct(this.currentId)
        .then(res => {
          this.list = res.data;
        })
        .catch(err => {
          this.$message.error(err.message);
        });
    },
    collect(id) {
      let that = this;
      if (!this.$store.state.access_token) {
        Toast({
          message: '登录后才可使用收藏和购物车功能！',
          duration: 3000,
          forbidClick: true
        });
        this.$router.push({ name: 'Login' });
      } else {
        let data = {
          type_id: id,
          type: 1
        };
        collectPro(data)
          .then(res => {
            if (res.status === 200) {
              that.$toastVant.success(res.message);
            }
          })
          .catch(err => {
            that.$message.error(err.message);
          });
      }
    },
    cart(id) {
      if (!this.$store.state.access_token) {
        Toast({
          message: '登录后才可使用收藏和购物车功能！',
          duration: 3000,
          forbidClick: true
        });
        this.$router.push({ name: 'Login' });
      } else {
        let data = {
          cart_num: 1,
          is_new: 0,
          product_id: id,
          product_type: 0
        };
        addCart(data)
          .then(res => {
            if (res.status === 200) {
              this.$toastVant.success('加入成功!');
            }
          })
          .catch(err => {
            this.$message.error(err.message);
          });
      }
    },
    getMenu() {
      let that = this;
      const id = 0;
      getProSpecialCate(id)
        .then(res => {
          that.menu = res.data;
          that.currentId = res.data[0] ? res.data[0].store_category_id : '';
          that.getList();
        })
        .catch(err => {
          that.$message.error(err.message);
        });
    }
  }
};
</script>

<style scoped>
@import '~@/assets/style/common.scss';
@import '~@/assets/style/base.css';

.topremmend {
  width: 100%;
  margin-top: 30px;
}

.topremmend .tabBox {
  display: flex;
  height: 60px;
  background: #e38b2e;
}

.topremmend .tabBox .menu-item.active {
  color: #fff;
  background: #0085e8 !important;
}

.topremmend .tabBox .item {
  font-size: 16px;
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #fff;
  padding: 0;
  cursor: pointer;
}

.heart {
  width: 20px;
  height: 20px;
  background: url('~@/assets/images/shoucanglogo.png') no-repeat;
  background-size: 45px;
  overflow: hidden;
  background-position: -2px 0;
}

.heart.active {
  background-position: -22px 0;
}
/* .topremmend .active {
  color: #fff;
  background: #0085e8;
} */

.panel-content {
  padding: 30px 22px 30px;
  background: #fff;
}

.panel-content .swiper {
  height: 634px;
  margin-left: auto;
  margin-right: auto;
  flex-wrap: wrap;
}

.panel-content .swiper-slide {
  height: 312px;
}

.panel-content .item {
  width: 100%;
  border: 1px solid #ccc;
  padding: 8px 8px 20px 8px;
}

.panel-content .item .pic-box {
  width: 100%;
  height: 167px;
}

.panel-content .item .pic-box img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.panel-content .item .item-title {
  margin: 17px 0;
  color: #333;
  height: 40px;
}
.panel-content .item:hover .item-title {
  color: #e38b2e;
}
.panel-content .item .price {
  font-weight: bold;
  color: #e38b2e;
}

.panel-content .item .cart {
  margin-top: 10px;
  font-weight: bold;
  color: #e38b2e;
}

.gray {
  color: #969696;
  margin-left: 5px;
  font-weight: normal;
}

.botton-con {
  position: relative;
  margin-top: 45px;
}

.into {
  position: absolute;
  bottom: 0;
  right: 22px;
  color: #0085e8;
  font-size: 16px;
}

.into span {
  margin-right: 14px;
}

.into img {
  vertical-align: bottom;
}

.swiper-container-multirow-column > .swiper-wrapper {
  flex-direction: row !important;
}

.panel-paginations {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
}

.panel-paginations /deep/ .swiper-pagination-bullet {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  margin-right: 16px;
  background: #ccc;
  opacity: 1;
  outline: none;
}

.panel-paginations /deep/ .swiper-pagination-bullet-active {
  background: rgba(228, 139, 44, 0.9);
  box-shadow: 0 0 10px rgba(228, 139, 44, 0.9);
}
</style>
